package com.yx.myinterest;

import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.content.Context;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.view.animation.LinearInterpolator;
import android.widget.ImageView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.constraintlayout.widget.ConstraintLayout;
import androidx.core.content.ContextCompat;

import java.util.Random;

/**
 * Creator:Yx
 * Time:2023/3/3 11:35
 * Description: 屏幕点赞 爱心动画
 */
public class LoveView extends ConstraintLayout {

    private Context mContext;
    // 随机爱心的旋转角度
    private float[] num = new float[]{-35f, -25f, 0f, 25f, 35f};

    // 判断是否是连续的点击事件
    // 上一次按下时间
    private static long mLastClickTime = 0;

    public LoveView(@NonNull Context context) {
        super(context);
        initView(context);
    }

    public LoveView(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        initView(context);
    }

    private void initView(Context context) {
        this.mContext = context;
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        //  当前时间
        long currentTime = System.currentTimeMillis();
        // 双击事件才绘制爱心
        // 两次点击的时间差
        long time = currentTime - mLastClickTime;
        if (time > 0 && time < 1000) {
            // 1秒内多次点击
            ImageView iv = new ImageView(mContext);
            // iv.setScaleType(ImageView.ScaleType.FIT_CENTER);
            // 设置展示图片的大小
            LayoutParams lp = new LayoutParams(200, 200);
            // 设置图片的相对坐标是父布局的左上角开始的
            lp.leftToLeft = 0;
            lp.topToTop = 0;

            // 设置图片相对于点击位置的坐标
            lp.leftMargin = (int) (event.getX() - 150F);
            lp.topMargin = (int) (event.getY() - 230F);

            // 设置图片资源
            iv.setImageDrawable(ContextCompat.getDrawable(mContext, R.drawable.icon_heart_red));
            iv.setLayoutParams(lp);

            //把IV添加到父布局中
            addView(iv);

            AnimatorSet animatorSet = new AnimatorSet();
            animatorSet.play(scaleAni(iv, "scaleX", 2f, 0.9f, 100L, 0L)) // 缩放动画，X轴2倍缩小至0.9倍
                    .with(scaleAni(iv, "scaleY", 2f, 0.9f, 100L, 0L)) // 缩放动画，Y轴2倍缩放至0.9倍
                    .with(rotation(iv, 0L, 0L, num[new Random().nextInt(4)])) // 旋转动画，随机旋转角
                    .with(alphaAni(iv, 0F, 1F, 100L, 0L)) // 渐变透明动画，透明度从0-1
                    .with(scaleAni(iv, "scaleX", 0.9f, 1F, 50L, 150L)) // 缩放动画，X轴0.9倍缩小至
                    .with(scaleAni(iv, "scaleY", 0.9f, 1F, 50L, 150L)) // 缩放动画，Y轴0.9倍缩放至
                    .with(translationY(iv, 0f, -600F, 800L, 400L)) // 位移动画，Y轴从0上移至600
                    .with(alphaAni(iv, 1F, 0F, 300L, 400L)) // 透明动画，从1-0
                    .with(scaleAni(iv, "scaleX", 1F, 3f, 700L, 400L)) // 缩放动画，X轴1至3倍
                    .with(scaleAni(iv, "scaleY", 1F, 3f, 700L, 400L)); // 缩放动画，Y轴1至3倍
            animatorSet.start();
            animatorSet.addListener(new AnimatorListenerAdapter() {
                @Override
                public void onAnimationEnd(Animator animation) {
                    super.onAnimationEnd(animation);
                    //当动画结束，把控件从父布局移除
                    removeViewInLayout(iv);
                }
            });
        } else {
            mLastClickTime = currentTime;
        }

        return super.onTouchEvent(event);
    }


    // vararg可变参数修饰符，此处可以传入多个Float类型值
    private ObjectAnimator rotation(View view, Long time, Long delayTime, float... values) {
        ObjectAnimator ani = ObjectAnimator.ofFloat(view, "rotation", values);
        ani.setDuration(time); // .duration = time
        ani.setStartDelay(delayTime); // .startDelay = delayTime
        ani.setInterpolator(input -> input);
        return ani;
    }

    private ObjectAnimator alphaAni(View view, Float from, Float to, Long time, Long delayTime) {
        ObjectAnimator ani = ObjectAnimator.ofFloat(view, "alpha", from, to);
        ani.setInterpolator(new LinearInterpolator());
        ani.setDuration(time);
        ani.setStartDelay(delayTime);
        return ani;
    }

    private ObjectAnimator translationY(View view, Float from, Float to, Long time, Long delayTime) {
        ObjectAnimator ani = ObjectAnimator.ofFloat(view, "translationY", from, to);
        ani.setInterpolator(new LinearInterpolator());
        ani.setDuration(time);
        ani.setStartDelay(delayTime);
        return ani;
    }

    private ObjectAnimator translationX(View view, Float from, Long time, Float to, Long delayTime) {
        ObjectAnimator ani = ObjectAnimator.ofFloat(view, "translationX", from, to);
        ani.setStartDelay(delayTime);
        ani.setDuration(time);
        ani.setInterpolator(new LinearInterpolator());
        return ani;
    }

    private ObjectAnimator scaleAni(View view, String propertyName, Float from, Float to, Long time, Long delayTime) {
        ObjectAnimator ani = ObjectAnimator.ofFloat(view, propertyName, from, to);
        ani.setInterpolator(new LinearInterpolator());
        ani.setStartDelay(delayTime);
        ani.setDuration(time);
        return ani;
    }

}
